﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BuXiaChu.OrderDishes.BLL;
using BuXiaChu.OrderDishes.Web.Admin.Modules.Sys;
using BuXiaChu.OrderDishes.BLL.WebRuntime;
using BuXiaChu.OrderDishes.Model;

namespace BuXiaChu.OrderDishes.Web.Admin.Sys
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class SysUser : HttpHandlerBase, IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            base.Init(context);
            var type = Request.Form["t"];
            switch (type)
            {
                case "1":
                    GetUsers();
                    break;
                case "2":
                    FindEmployees();
                    break;
                case "3":
                    AddSysUser();
                    break;
                case "4":
                    DeleteUser();
                    break;
                default: break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #region

        private void GetUsers()
        {
            AjaxActionResult result = new AjaxActionResult();
            UserControlPreviewManager<SysUserList> preivewManager = new UserControlPreviewManager<SysUserList>();
            SysUserList sysUserList = preivewManager.LoadUserControl("~/Modules/Sys/SysUserList.ascx");
            sysUserList.List = SysUserManager.GetSysUsers().OrderBy(item => item.LoginName).ToList<SysUserInfoModel>();

            result.message = preivewManager.RenderHtml(sysUserList);
            result.success = true;
            SerializeDataToResponseStream(result);
        }

        private void FindEmployees()
        {
            var sqlWhere = " 1=1 ";
            var name = GetFormValue("userName");
            if (string.IsNullOrEmpty(name) == false)
            {
                sqlWhere += " and name like'%" + name.Trim() + "%'";
            }

            AjaxActionResult result = new AjaxActionResult();
            UserControlPreviewManager<AddEmployeeToSysUser> preivewManager = new UserControlPreviewManager<AddEmployeeToSysUser>();
            AddEmployeeToSysUser employees = preivewManager.LoadUserControl("~/Modules/Sys/AddEmployeeToSysUser.ascx");

            int pageCount;
            employees.List = EmployeesManager.Search(sqlWhere, 0, 0, out pageCount);
            result.message = preivewManager.RenderHtml(employees);
            result.success = true;
            SerializeDataToResponseStream(result);
        }

        private void AddSysUser()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            var userName = GetFormValue("userName");
            var password = GetFormValue("password");
            var appId = GetFormValue("appId");
            var empId = GetFormValue("empId");

            if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
            {
                SysUserInfoModel user = new SysUserInfoModel();
                user.ApplicationId = new Guid(appId);
                user.EmployeeId = int.Parse(empId);
                user.LoginName = userName;
                user.Password = password;

                result.success = SysUserManager.Save(user);
            }
            SerializeDataToResponseStream(result);
            #endregion
        }

        private void DeleteUser()
        {
            AjaxActionResult result = new AjaxActionResult();
            var id = GetFormValue("id");
            var systemUserId = 0;

            if (!string.IsNullOrEmpty(id) && int.TryParse(id, out systemUserId) && systemUserId > 0)
            {
                result.success = SysUserManager.Delete(systemUserId);
            }
            SerializeDataToResponseStream(result);
        }


        #endregion
    }
}
